﻿using BizModels;
using DataModel;
using IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Repositories
{
    public class BoardAutoSaveRepository : RepositoryBase, IBoardAutoSaveRepository
    {
        public BoardAutoSaveRepository()
        {
            Context = new HinokyEntities();
        }

        public int AddBoardAutoSave(BizModels.BoardAutoSaveT board)
        {
            var data = new BoardAutoSave
            {
                UserKey = board.UserKey,
                Title = board.Title,
                Contents = board.Contents,
                SearchTag = board.SearchTag,
                InsertDate = DateTime.Now
            };

            Context.BoardAutoSave.Add(data);

            Context.SaveChanges();

            return data.TempIdx;
        }

        public BoardAutoSaveT GetBoardAutoSave(int idx, int userKey)
        {
            var boardAutoSaveT = new BoardAutoSaveT();

            var data = Context.BoardAutoSave.SingleOrDefault(s => s.TempIdx.Equals(idx) && s.UserKey.Equals(userKey));

            if (data == null) return boardAutoSaveT;

            boardAutoSaveT.TempIdx = data.TempIdx;
            boardAutoSaveT.Title = data.Title;
            boardAutoSaveT.Contents = data.Contents;
            boardAutoSaveT.SearchTag = data.SearchTag;
            boardAutoSaveT.InsertDate = data.InsertDate;
            boardAutoSaveT.UserKey = data.UserKey;

            return boardAutoSaveT;
        }

        public List<BizModels.BoardAutoSaveT> GetBoardAutoSaveList(int userKey)
        {
            return Context.BoardAutoSave.Where(s => s.UserKey == userKey).Select(s => new BoardAutoSaveT
            {
                TempIdx = s.TempIdx,
                Title = s.Title,
                Contents = s.Contents,
                SearchTag = s.SearchTag,
                InsertDate = s.InsertDate,
                UserKey = s.UserKey
            }).ToList();
        }

        public int SetBoardAutoSave(BizModels.BoardAutoSaveT boardAutoSaveT)
        {
            var data = Context.BoardAutoSave.SingleOrDefault(s => s.TempIdx.Equals(boardAutoSaveT.TempIdx));

            if (data == null) return 0;

            data.TempIdx = boardAutoSaveT.TempIdx;
            data.Title = boardAutoSaveT.Title;
            data.Contents = boardAutoSaveT.Contents;
            data.SearchTag = boardAutoSaveT.SearchTag;
            data.InsertDate = DateTime.Now;
            data.UserKey = boardAutoSaveT.UserKey;

            Context.SaveChanges();

            return data.TempIdx;
        }

        public int RemoveBoardAutoSave(int idx, int userKey)
        {
            var data = Context.BoardAutoSave.SingleOrDefault(s => s.TempIdx == idx && s.UserKey == userKey);

            if (data == null) return 0;

            Context.BoardAutoSave.Remove(data);

            return Context.SaveChanges();
        }
    }
}
